package com.uniriho.lxr0811_7300_bl.ui.activity

import android.Manifest
import android.bluetooth.BluetoothDevice
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.uniriho.lxr0811_7300_bl.adapter.BleDevAdapter
import com.uniriho.lxr0811_7300_bl.ble.BleCon
import com.uniriho.lxr0811_7300_bl.databinding.ActivityMainBinding

class MainActivity: AppCompatActivity(), BleDevAdapter.Listener,
    SwipeRefreshLayout.OnRefreshListener {
    private val mainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }
    private lateinit var freshLayout: SwipeRefreshLayout
    private lateinit var mainViewModel:MainViewModel
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(mainBinding.root)
intiView()
        initData()
        initEvent()
    }

    private fun intiView() {
        freshLayout = mainBinding.freshLayout

    }

    private fun initEvent() {
        freshLayout.setOnRefreshListener(this)

    }

    private fun initData() {

        // Android 6.0动态请求权限
        val permissions = arrayOf(
            Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION
        )
        for (str in permissions) {
            if (checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(permissions, 111)
                break
            }
        }
        mainViewModel  =MainViewModel(mainBinding,this)

    }

    override fun onItemClick(dev: BluetoothDevice?) {

    }

    override fun onRefresh() {
        mainViewModel.reScanBleDev()
    }
}